package com.skt.common_hr.healthreq.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.healthreq.HealthreqFacade;
	import com.skt.common_hr.healthreq.model.AttachFileProxy;
	import com.skt.common_hr.healthreq.model.NoticeViewProxy;
	import com.skt.common_hr.healthreq.model.NoticeWriteProxy;
	import com.skt.common_hr.healthreq.model.vo.NoticeDetailVO;
	import com.skt.common_hr.healthreq.model.vo.NoticeVO;
	import com.skt.common_hr.healthreq.view.components.NoticeWrite;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class NoticeWriteMediator extends CommonMediator
	{
		public static const NAME:String = "NoticeWriteMediator";
		
		private const MODE_WAITING:String = "modeWaiting";
		private const MODE_WRITING:String = "modeWriting";
		private const MODE_MODIFY:String = "modeModify";
		
		private var _mode:String = MODE_WAITING;
		private var _bWaitingSave:Boolean = false;
		private var orgFileName:String = ""; 
		
		public function NoticeWriteMediator(viewCompo:Object)
        {
            super( NAME, viewCompo );
        }
		
		override protected function initializeView():void
		{
			super.initializeView();
		}
		
		private function setEventListener():void
		{
			view.addEventListener(NoticeWrite.NoticeSave, onNoticeSave);
			
			view.addEventListener(NoticeWrite.NoticeCancel, onCancel);
			view.addEventListener(NoticeWrite.GetFile, onBrowseFile);
			
			view.addEventListener(NoticeWrite.replySave, onReplySave);			
		}
		
		private function removeEventListener():void
		{
			view.removeEventListener(NoticeWrite.NoticeSave, onNoticeSave);
			
			view.removeEventListener(NoticeWrite.NoticeCancel, onCancel);
			view.removeEventListener(NoticeWrite.GetFile, onBrowseFile);
			
			view.removeEventListener(NoticeWrite.replySave, onReplySave);
		}
		
		override protected function clearView():void
		{
			view.dataProvider = null;
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					HealthreqFacade.MODE_WRITE_NOTICE,
					HealthreqFacade.MODE_MODIFY_NOTICE,
					HealthreqFacade.INSERT_NOTICE_COMPLETE,
					HealthreqFacade.INSERT_NOTICE_FAILED,
					HealthreqFacade.UPDATE_NOTICE_COMPLETE,
					HealthreqFacade.UPDATE_NOTICE_FAILED

					];
		}
		
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case HealthreqFacade.MODE_WRITE_NOTICE:
					startWriting();
					break;
				case HealthreqFacade.MODE_MODIFY_NOTICE:
					startModify(note.getBody() as NoticeVO);
					break;
				case HealthreqFacade.INSERT_NOTICE_COMPLETE:
					stopWriting();
					break;
				case HealthreqFacade.INSERT_NOTICE_FAILED:
					//CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;
					
				case HealthreqFacade.UPDATE_NOTICE_COMPLETE:
					stopWriting();
					break;
				case HealthreqFacade.UPDATE_NOTICE_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;
			}
		}		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	MVC Message handlers
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		// E:구성원, M:관리자  
		public function checkRoleForView():void {
			if(view.sRole == 'E') {
			} else if(view.sRole == 'M') {
			}
		}
		
		private function startWriting():void
		{
			_mode = MODE_WRITING;
			setEventListener();
			orgFileName = "";
			view.dataProvider = null;
			
			view.cbT_HEGBN.selectedIndex = 0;
			view.onChange_cbT_HEGBN();
			
		}
		
		private function startModify(vo:NoticeVO):void
		{ 
			_mode = MODE_MODIFY;
			setEventListener();			
			view.dataProvider = vo;
			
			/* 
			checkRoleForView();
						
			// 임시저장 상태라면 
			if(vo.HLSTA == "1") {
				view.hbWriteBox.visible	= true;
			}
			else  {
				view.hbWriteBox.visible	= false;
			}
			 */
		}
		
		private function stopWriting():void
		{
			_mode = MODE_WAITING;
 			removeEventListener();
 			clearView();
 			facade.sendNotification(HealthreqFacade.MODE_LIST_NOTICE, true);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onCancel(e:Event):void
		{
			trace("onCancel");
			removeEventListener();
			facade.sendNotification(HealthreqFacade.MODE_LIST_NOTICE, false);
		}
		
		
		private function buildSaveData():NoticeVO
		{
			var vo:NoticeVO = new NoticeVO();
 			
 			// 수정시에만 가지고 있는 값. 
			vo.HENUM		= view._dataProvider.HENUM;
			
			vo.MEFAM		= view.tiMEFAM.text;
			vo.MEFNM		= view.tiMEFNM.text;
			vo.MEREG			= view.tiMEREG.text;
			vo.MREDAT		= DateUtil.toBasicString(view.dfMREDAT.selectedDate);
			
			vo.AMEBET		= view.ntiAmt.dataValue;
			
			if(view.cbT_HEGBN.selectedItem!=null) {
				vo.HEGBN			= view.cbT_HEGBN.selectedItem.CODE;
			}
			if(view.cbT_MEDCK.selectedItem!=null) {
				vo.MEDCK		= view.cbT_MEDCK.selectedItem.CODE;		
			}
			if(view.cbT_SCODE.selectedItem!=null) {
				vo.MESTO			= view.cbT_SCODE.selectedItem.CODE;
			}
			if(view.cbT_RCODE.selectedItem!=null) {
				vo.STATE			= view.cbT_RCODE.selectedItem.CODE;
			}
			if(view.cbT_HCODE.selectedItem!=null) {
				vo.STCD2			= view.cbT_HCODE.selectedItem.CODE;
			}									
			
			
			
			
			if(view.cbT_MEDCD1.selectedItem!=null) {
				vo.MEDCD01		= view.cbT_MEDCD1.selectedItem.CODE;
			}
			
			if(view.cbT_MEDCD2.selectedItem!=null) {
				vo.MEDCD02		= view.cbT_MEDCD2.selectedItem.CODE;
			}
			if(view.cbT_MEDCD3.selectedItem!=null) {
				vo.MEDCD03		= view.cbT_MEDCD3.selectedItem.CODE;
			}
			if(view.cbT_MEDCD4.selectedItem!=null) {
				vo.MEDCD04		= view.cbT_MEDCD4.selectedItem.CODE;
			}
			if(view.cbT_MEDCD5.selectedItem!=null) {
				vo.MEDCD05		= view.cbT_MEDCD5.selectedItem.CODE;
			}
			if(view.cbT_MEDCD6.selectedItem!=null) {
				vo.MEDCD06		= view.cbT_MEDCD6.selectedItem.CODE;
			}												
			
						
			
			
			
			
			
			
			
			
			
			
			
			
			if(view.cbT_AMEDCD1.selectedItem!=null) {
				vo.AMED01		= view.cbT_AMEDCD1.selectedItem.CODE;
			}
			
			if(view.cbT_AMEDCD2.selectedItem!=null) {
				vo.AMED02		= view.cbT_AMEDCD2.selectedItem.CODE;
			}
			
			if(view.cbT_AMEDCD3.selectedItem!=null) {
				vo.AMED03		= view.cbT_AMEDCD3.selectedItem.CODE;
			}
			
			if(view.cbT_AMEDCD4.selectedItem!=null) {
				vo.AMED04		= view.cbT_AMEDCD4.selectedItem.CODE;	
			}
			
			if(view.cbT_AMEDCD5.selectedItem!=null) {
				vo.AMED05		= view.cbT_AMEDCD5.selectedItem.CODE;
			}

			vo.AGBN = view.rbgAddr.selectedValue.toString();
			vo.ADDR = view.tiADDR.text;


// 			vo.I_TITLE				= view.titleNm.text;
// 			vo.I_CONTENT		= view.content.htmlText;
// 			vo.I_CONTX			= view.content.text;
 			
// 			vo.I_WRITER_PERNR	= Variables.myPernr;
// 			vo.I_BEGDA			= view.Ibegda.text;
// 			vo.I_ENDDA			= view.Iendda.text;
 		
 			return vo;
		}
		
 		//private var vo:NoticeDetailVO = new NoticeDetailVO();
 		private function onNoticeSave(event:Event):void
 		{
 			if(!validateFields()) {
 				return;
 			}
 			saveNotice(); 
 		}
		
 		private function onReplySave(event:Event):void
 		{
 			var obj:Object = {};

 			obj.HLNUM		= view.writeKey;
// 			obj.RCONTENT	= view.rcontent.htmlText;
// 			obj.RCONTX		= view.rcontent.text;
 			var proxy:NoticeWriteProxy = facade.retrieveProxy(NoticeWriteProxy.NAME) as NoticeWriteProxy;
 			proxy.reply_insert(obj, view.sInsertType);
 		}		
		
		private function saveNotice():void 
		{
			_bWaitingSave = false;
			var proxy:NoticeWriteProxy = facade.retrieveProxy(NoticeWriteProxy.NAME) as NoticeWriteProxy;
			
 			if(_mode == MODE_WRITING) {	
	 			proxy.insert(buildSaveData(), view.sInsertType);
 			}
 			else {
				proxy.update(buildSaveData(), view.sInsertType); 				
 			}
 			
		}
		private function onBrowseFile(event:Event):void
		{
			var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
			proxy.browse();
		} 
		 
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function validateFields():Boolean
		{
//			if(view.titleNm.text == "") {
//				CommonAlert.showMessage("Notice", "ALERT_NO_TITLE");
//				return false;
//			}
			
			return true;
		}
		
        private function get view():NoticeWrite
        {
            return viewComponent as NoticeWrite;
        }
	}
}